import "mod_text"
import "mod_wm"
import "mod_map"
import "mod_proc"
import "mod_video"
import "mod_rand"
import "mod_screen"
import "mod_draw"

include "../../commons/global.prg"
include "../../commons/keys_control.prg"

Const
  TICTACTOE_WIDTH = 3;
  TICTACTOE_HEIGHT = 3;
  TICTACTOE_WIDTH_TILE =20;
  TICTACTOE_HEIGHT_TILE =20;
  TICTACTOE_SQUARE =0;
  TICTACTOE_CIRCLE = 1;
End
    Type TICTACTOE_point
        int x;
        int y;
    End

Global
  TICTACTOE_main_board TICTACTOE_board; 
  int TICTACTOE_turn;
End

Begin
    initialice_game();
	TICTACTOE_main();
End

/* Men� b�sico */
Process TICTACTOE_main()
Begin
    TICTACTOE_turn = PLAYER_1;
    write(0,400,200,1,"Tic Tac Toe");
    write(0,400,450,1,"Pulsa intro para Empezar");
    write(0,400,470,1,"o escape Salir");
    While (!is_key_pressed(PLAYER_1,KEY_START))  
        If(is_key_pressed(PLAYER_1,KEY_CLOSE))
          While (is_key_pressed(PLAYER_1,KEY_CLOSE))
            Frame;
          End
          let_me_alone();
          delete_text(all_text);
          //menu();
          return;
        End //Acabar la ejecuci�n si se ha seleccionado la tecla SELECT
        Frame;
    End                            
    delete_text(all_text);
    //Y comienza el juego (por fin!)
    TICTACTOE_game();
end

Process TICTACTOE_game()
Private
  int id_text =0;
Begin
  TICTACTOE_board = TICTACTOE_main_board();
  TICTACTOE_player();
  TICTACTOE_computer();
  loop
    //Si se pulsa CLOSE mientras se est� jugando, se para todo y se sale al men� inicial 
    if(is_key_pressed(PLAYER_1,KEY_close))
      while(is_key_pressed(PLAYER_1,KEY_close))
        frame;
      end //Hasta que no se suelte la tecla no se hace nada
      clear_screen();
      delete_text(all_text);
      let_me_alone(); //Deja s�lo el proceso actual
      TICTACTOE_main(); //Pone en marcha el men� otra vez
      return; /*Ahora hay dos procesos, el actual (que ya no interesa), y el men�. Una manera de acabar con el proceso actual -un suicidio- es con esta l�nea.*/
    end
    If (is_winner(PLAYER_1))
            id_text=write(0,300,280,4,"Has ganado. Pulsa START");
            While (!is_key_pressed(PLAYER_1,KEY_START))
                Frame;
            End
            While (is_key_pressed(PLAYER_1,KEY_START))
                Frame;
            End
            let_me_alone();
            TICTACTOE_main();
            Break;
    End
    If (is_winner(PLAYER_2))
            id_text=write(0,300,280,4,"Has perdido. Pulsa START");
            While (!is_key_pressed(PLAYER_1,KEY_START))
                Frame;
            End
            While (is_key_pressed(PLAYER_1,KEY_START))
                Frame;
            End
            let_me_alone();
            TICTACTOE_main();
            Break;
    End
    frame;
  End
  if (id_text !=0 ) delete_text(id_text); end                                   
End

process TICTACTOE_main_board()
Public
    int data[TICTACTOE_WIDTH][TICTACTOE_HEIGHT];
Private
  int i,j;
  int block_color;
Begin
  //Se vac�a el campo de juego
  for(i=0;i<TICTACTOE_WIDTH;i++)
    for(j=0;j<TICTACTOE_HEIGHT;j++)
      data[i][j] = 0;
    end
  end
  graph=new_map((TICTACTOE_WIDTH+2)*TICTACTOE_WIDTH_TILE,(TICTACTOE_HEIGHT+2)*TICTACTOE_HEIGHT_TILE,SCREEN_DEPTH);
  x=300;
  y=200;
  while (!is_key_pressed(PLAYER_1,KEY_CLOSE))
    map_clear(0,graph,0);
    //Dibujo el campo de juego
    for(i = 0;i < TICTACTOE_WIDTH+2;i++)
      for(j = 0;j < TICTACTOE_HEIGHT+2;j++)
        //De entrada, la casilla l�gica est� vac�a 
        block_color = 0;
        //Si la casilla pertenece al borde, la pinto con el color gris claro
        if( i == 0  or (i == TICTACTOE_WIDTH+1) or j==0 or (j == TICTACTOE_HEIGHT+1) ) 
          block_color = rgb(170,170,170);
        else
          //Al principio del juego, todos los elementos de TICTACTOE_board[][] valen 0 (es decir, no hay ninguna casilla l�gica ocupada). 
          //A medida que se vaya jugando, block_color ir� valiendo en tiempo real el color que se ha de pintar en la casilla l�gica
          block_color = data[i-1][j-1];
        end
        if (block_color != 0)
          TICTACTOE_print_block(0,graph, block_color,i,j,TICTACTOE_SQUARE);
        end
      end
    end
    frame;
  end
end

Process TICTACTOE_computer()
Private
    int i,j;
Begin
  While (!is_key_pressed(PLAYER_1,KEY_CLOSE))
    If (TICTACTOE_turn==PLAYER_2)
        i=0;
        While (i<TICTACTOE_WIDTH AND TICTACTOE_turn==PLAYER_2)
           j=0;
           While(j<TICTACTOE_HEIGHT AND TICTACTOE_turn==PLAYER_2)
            If (TICTACTOE_board.data[i][j]==0)
                TICTACTOE_board.data[i][j] = RGB(0,0,255);
                TICTACTOE_turn = PLAYER_1;
                break;
            End
            j++;
           End
           i++;
        End
    End
    Frame;  
  End
End

process TICTACTOE_player()
private
  int i,j,k,l;
  TICTACTOE_point cur_pos;
end
begin
  z=-255;
  graph=new_map(TICTACTOE_WIDTH_TILE,TICTACTOE_HEIGHT_TILE,SCREEN_DEPTH);
  map_clear(0,graph,0);
  TICTACTOE_print_block(0,graph,RGB(255,0,0),0,0,TICTACTOE_CIRCLE);
  alpha = 100; 
  while (!is_key_pressed(PLAYER_1,KEY_CLOSE))
    x=cur_pos.x*TICTACTOE_WIDTH_TILE+280;
    y=cur_pos.y*TICTACTOE_HEIGHT_TILE+180;
    If(is_key_pressed(PLAYER_1,KEY_UP))
        If (cur_pos.y-1 >= 0)
          cur_pos.y--;
        End
    End
    If(is_key_pressed(PLAYER_1,KEY_DOWN))
        If (cur_pos.y+1 < TICTACTOE_HEIGHT)
          cur_pos.y++;
        End
    End
    If(is_key_pressed(PLAYER_1,KEY_LEFT))
        If (cur_pos.x-1 >= 0)
          cur_pos.x--;
        End
    End
    If(is_key_pressed(PLAYER_1,KEY_RIGHT))
        If (cur_pos.x+1 < TICTACTOE_WIDTH)
          cur_pos.x++;
        End
    End
    If(is_key_pressed(PLAYER_1,KEY_A) AND TICTACTOE_turn == PLAYER_1)
       if (TICTACTOE_board!=0 AND TICTACTOE_board.data[cur_pos.x][cur_pos.y] ==0)
        TICTACTOE_board.data[cur_pos.x][cur_pos.y] = RGB(255,0,255);
        TICTACTOE_turn = PLAYER_2;
       End
    End
    Frame(250);
  End
End

function TICTACTOE_print_block(int file,int graph, int color,int x, int y, int id_type )
private
  int block_map,block_border_map;
begin
  block_map = new_map(TICTACTOE_WIDTH_TILE-2,TICTACTOE_HEIGHT_TILE-2,16);
  if (id_type == TICTACTOE_CIRCLE)
      Drawing_map(0,block_map);
      drawing_color(color);
      draw_fcircle(9,9,8);
  else
      map_clear(0,block_map,color);
  end
  block_border_map = new_map(TICTACTOE_WIDTH_TILE,TICTACTOE_HEIGHT_TILE,SCREEN_DEPTH);
  map_clear(0,block_border_map,RGB(0,0,0));
  map_xputnp(0,block_border_map,0,block_map,10,10,0,100,100,0);  
  map_xputnp(0,graph,0,block_map,10+TICTACTOE_WIDTH_TILE*x,10+TICTACTOE_HEIGHT_TILE*y,0,100,100,0);
  unload_map(0,block_map);
  unload_map(0,block_border_map);
end

function is_winner(int player)
Private
    int i,j;
    int joinned;
    int color;
Begin
        if (player == PLAYER_1)
            color = RGB(255,0,255);
        else
            color = RGB(0,0,255);
        end
        i=0;
        While (i<TICTACTOE_WIDTH )
           j=0;
           joinned = 0;
           While(j<TICTACTOE_HEIGHT AND TICTACTOE_board.data[i][j]== color)
            joinned++;
            If (joinned >=3)
              return TRUE;
            End
            j++;
           End
           i++;
        End
        j=0;
        While (j<TICTACTOE_HEIGHT)
           i=0;
           joinned = 0;
           While(i<TICTACTOE_HEIGHT AND TICTACTOE_board.data[i][j]== color)
            joinned++;
            If (joinned >=3)
                return TRUE;
            End
            i++;
           End
           j++;
        End
        if ( TICTACTOE_board.data[0][0] == color
                 AND TICTACTOE_board.data[1][1] == color 
                 AND TICTACTOE_board.data[2][2] == color )
                return TRUE;
        End
        if ( TICTACTOE_board.data[0][2] == color 
                 AND TICTACTOE_board.data[1][1] == color 
                 AND TICTACTOE_board.data[2][0] == color )
                return TRUE;
        End
    return FALSE;
End
